package com.tucanstubs.android_wheresmystuff.activities;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import com.tucanstubs.android_wheresmystuff.R;
import com.tucanstubs.android_wheresmystuff.database.ItemHelper;
import com.tucanstubs.android_wheresmystuff.database.SearchHelper;
import com.tucanstubs.android_wheresmystuff.items.Item;
import com.tucanstubs.android_wheresmystuff.items.ItemType;
import com.tucanstubs.android_wheresmystuff.items.LostItem;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Activity for performing searches. The user can can enter information and search for matching items.
 * The user can also reach this screen from the my Items screen, if they clicked "search for similar"
 * items on that screen. If this is the case all of the fields will be filled in with that items info.
 * 
 * @author team 37
 * @version 2.0 4/9/2013
 *
 */
public class SearchItemActivity extends Activity {
	private EditText itemName;
	private Date start,end;
	private ItemType itemType;
	private String userName;
	
	/**
	 * Creates the activity. Sets up all GUI components and initializes search fields to proper values.
	 * The activity receives a bundle with the keys Tags.USERNAME, and TAGS.ITEM.
	 * Tags.USERNAME maps to the user name of the currently logged in user. Tags.ITEM maps to either -1
	 * or an item id. If it is -1 then this activity was launched from the home screen and all fields 
	 * are left to defaults. If it is an itemID then this screen was reached by clicking the 
	 * "search for similar items" button and the fields are filled in to match that items info.
	 * 
	 * @param SavedInstanceState not used
	 * 
	 */
	public void onCreate(Bundle SavedInstanceState){
		super.onCreate(SavedInstanceState);
		setContentView(R.layout.search_screen);
		Bundle bundle=getIntent().getExtras();
		//initialize all fields to defaults
		itemName = (EditText) findViewById(R.id.Search_Enter_Item_Name);
		end=new Date();
		itemType = ItemType.HEIRLOOM;
	    Calendar calendar=Calendar.getInstance();
	    calendar.setTime(end);
		calendar.add(Calendar.DAY_OF_YEAR, -7);
		start=calendar.getTime();
		int itemID=bundle.getInt(Tags.ITEM);
		userName = bundle.getString(Tags.USERNAME);
		if(itemID >= 1){
			//see if we were passed in an item from the previous activity, 
			//(i.e. we came from the my items list screen)
			//if so we need to update the search screen so that items info is displayed
			
			Item searchItem = null;
			searchItem = ItemHelper.getItem(this, itemID);
			
			itemName.setText(searchItem.getName());
			if(searchItem instanceof LostItem)
				itemType=((LostItem)searchItem).getItemType();
			else
				itemType=ItemType.MISC;
			switch(itemType){
				case MISC:
					((RadioButton)findViewById(R.id.Misc)).setChecked(true);
					((RadioButton)findViewById(R.id.Heirloom)).setChecked(false);
					((RadioButton)findViewById(R.id.KeepSake)).setChecked(false);
					break;
				case HEIRLOOM:
					((RadioButton)findViewById(R.id.Heirloom)).setChecked(true);
					((RadioButton)findViewById(R.id.KeepSake)).setChecked(false);
					((RadioButton)findViewById(R.id.Misc)).setChecked(false);
					break;
				case KEEPSAKE:
					((RadioButton)findViewById(R.id.KeepSake)).setChecked(true);
					((RadioButton)findViewById(R.id.Heirloom)).setChecked(false);
					((RadioButton)findViewById(R.id.Misc)).setChecked(false);
					break;
					
			}
		}
	}
	
	/**
	 * Method that is called when a button is clicked. Either a search is performed to
	 * find items matching the inputed information, or the search is cancelled.
	 * 
	 * @param view The Button which was clicked on
	 */
	public void onClick(View view){
		//find which button was clicked
		switch (view.getId()) {
		//if search was clicked
		case R.id.Seach_Item_Button:
			//make a list of all relevant info to pass to the
			//database helper class
			List<String> itemInfo=new ArrayList<String>();
			itemInfo.add(itemName.getText()==null ? "": itemName.getText().toString());
			itemInfo.add(itemType.name());
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			itemInfo.add(format.format(start));
			itemInfo.add(format.format(end));
			itemInfo.add(userName);
			//get results from database
			List<Item> matchedItems=SearchHelper.searchItems(this, itemInfo);
			//put item ids int[] for next activity to display
			int[] matchedItemIDs=new int[matchedItems.size()];
			for(int i=0; i<matchedItemIDs.length; i++)
				matchedItemIDs[i]=matchedItems.get(i).getId();
			//prepare bundle and pass control to next activity
			Bundle bundle=new Bundle();
			bundle.putIntArray(Tags.ITEMIDS, matchedItemIDs);
			bundle.putString(Tags.CLASS,"SearchItemActivity");
			bundle.putString(Tags.USERNAME, userName);
			Intent nextIntent = new Intent(this,ItemListActivity.class);
			nextIntent.putExtras(bundle);
			startActivity(nextIntent);
			break;
		//if cancel was clicked, just end acitivty
		case R.id.Cancel_Search_Button:
			finish();
			break;
		}
	}
	
	/**
	 * Called when a date radio button is clicked, updates the start date accordingly.
	 * @param view Which radio button was clicked
	 */
	public void onDateClicked(View view) {
	    boolean checked = ((RadioButton) view).isChecked();
	    //calendar object for setting the date in the past
	    Calendar calendar=Calendar.getInstance();
	    calendar.setTime(end);
	    //find which radio button was clicked and set start date accordingly
	    switch(view.getId()){
	    case R.id.Week:
	    	if(checked)
	    		calendar.add(Calendar.DAY_OF_YEAR, -7);
	    		start=calendar.getTime();
	    		
	    	break;
	    case R.id.Weeks:
	    	calendar.add(Calendar.DAY_OF_YEAR, -14);
	    	start=calendar.getTime();
	    	break;
	    case R.id.Month:
	    	calendar.add(Calendar.DAY_OF_YEAR, -30);
	    	start=calendar.getTime();
	    	break;
	    }
	}
	/**
	 * Method which is called when an item category radio button is clicked. Updates the current
	 * item type.
	 * @param view Which radio button was clicked
	 */
	public void onCategoryClicked(View view) {
	    boolean checked = ((RadioButton) view).isChecked();
	    //find which radio button was clicked and set the itemType accordingly
	    switch(view.getId()){
	    case R.id.Heirloom:
	    	if(checked)
	    		itemType=ItemType.HEIRLOOM;
	    	break;
	    case R.id.Misc:
	    	if(checked)
	    		itemType=ItemType.MISC;
	    	break;
	    case R.id.KeepSake:
	    	if(checked)
	    		itemType=ItemType.KEEPSAKE;
	    	break;
	    }
	}
}
